using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using BriansClimb;
using BriansClimb.GameObjects;
using BriansClimb.Climber;
using BriansClimb.DrawableGameObjects.Buttons;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;

namespace BriansClimb.DrawableGameObjects.GameScenes
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class WallSelectionScene : GameScene
    {
        protected Game game;

        protected SpriteBatch spriteBatch;
        protected AudioManager audioManager;

        protected Texture2D easyTab;
        protected Texture2D mediumTab;
        protected Texture2D hardTab;
        protected Texture2D customTab;        
        
        //!easyWindow shows the easy climb
        protected Texture2D easyWindow;
        
        //!mediumWindow shows the medium climb
        protected Texture2D mediumWindow;
        
        //!hardWindow shows the hard climb
        protected Texture2D hardWindow;

        //!customWindow displays a list of the available custom climbs
        protected Texture2D customWindow;
        
        //!activeWindow holds the current window being displayed
        protected Texture2D activeWindow;
        protected Texture2D background;

        protected SpriteFont tabFont;

        //These ranges control the field the mouse must be in to select them on the screen
        protected Vector2 rangeWidth;
        protected Vector2 rangeEasy;
        protected Vector2 rangeMedium;
        protected Vector2 rangeHard;
        protected Vector2 rangeCustom;

        protected bool clicked;
        protected bool pressed;
        protected bool quickPlay = false;

        protected string windowName;

        protected MouseState mouseState;

        public LeftClickButton nextButton;
        public LeftClickButton backButton;

        protected TextMenuComponent customMenu;

        protected FileManager fileManager;
        protected FileStatManager fileStatManager;
        
        /// <summary>
        /// The default constructor
        /// FileManager is used to access the file list for customWindow
        /// Loads textures for the tabs and windows, loads the buttons
        /// Sets the default window
        /// </summary>
        /// <param name="game"></param>
        /// <param name="fileManager"></param>
        /// <param name="fileStatManager"></param>
        /// <param name="background"></param>
        /// <param name="normalFont"></param>
        /// <param name="selectedFont"></param>
        public WallSelectionScene(Game game, FileManager fileManager, FileStatManager fileStatManager, Texture2D background,
                                  SpriteFont normalFont, SpriteFont selectedFont)
            : base(game)
        {
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            audioManager = (AudioManager)game.Services.GetService(typeof(AudioManager));

            this.fileManager = fileManager;
            this.background = game.Content.Load<Texture2D>(".\\Textures\\BackgroundWallSelection");
            this.game = game;
            this.fileStatManager = fileStatManager;

            easyTab =   game.Content.Load<Texture2D>(".\\Textures\\easyTab");
            mediumTab = game.Content.Load<Texture2D>(".\\Textures\\mediumTab");
            hardTab =   game.Content.Load<Texture2D>(".\\Textures\\hardTab");
            customTab = game.Content.Load<Texture2D>(".\\Textures\\customTab");

            easyWindow = game.Content.Load<Texture2D>(".\\Textures\\EasyWindow");
            mediumWindow = game.Content.Load<Texture2D>(".\\Textures\\MediumWindow");
            hardWindow = game.Content.Load<Texture2D>(".\\Textures\\HardWindow");
            customWindow = game.Content.Load<Texture2D>(".\\Textures\\CustomWindow");

            windowName = "easyWindow";
            activeWindow = easyWindow;

            mouseState = Mouse.GetState();

            nextButton = new LeftClickButton(game, "next", 1075, 50);
            Components.Add(nextButton);

            backButton = new LeftClickButton(game, "back", 50, 50);
            Components.Add(backButton);

            fileManager.GetListOfFileName();
            string[] fileNameArray = fileManager.FileList.ToArray();         
            customMenu = new TextMenuComponent(game, normalFont, selectedFont);
            customMenu.SetMenuItems(fileNameArray);
            Components.Add(customMenu);

            //Sets the horizontal width, the other ranges are vertical
            rangeWidth.X = 100;            
            rangeWidth.Y = 250;

            rangeEasy.X = 178;
            rangeEasy.Y = 328;

            rangeMedium.X = 344;
            rangeMedium.Y = 494;

            rangeHard.X = 509;
            rangeHard.Y = 659;

            rangeCustom.X = 674;
            rangeCustom.Y = 824;            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        public string NameOfWindow
        {
            get { return windowName; }
        }

        public TextMenuComponent CustomLoadTab
        {
            get { return customMenu; }
        }

        public bool QuickPlay
        {
            get { return quickPlay; }
            set { quickPlay = value; }
        }

        /// <summary>
        /// Show the Wall Customize Scene
        /// </summary>
        public override void Show()
        {
            base.Show();
        }

        /// <summary>
        /// Hide the Wall Customize Scene
        /// </summary>
        public override void Hide()
        {
            activeWindow = easyWindow;

            HideCustomMenu();
            base.Hide();
        }

        /// <summary>
        /// Shows the 'esc' menu.
        /// </summary>
        private void ShowCustomMenu()
        {
            // Put the menu centered in screen
            customMenu.Position = new Vector2(400, 150);

            //set the 'esc' menu to be visible
            customMenu.Visible = true;
            customMenu.Enabled = true;

        }

        private void HideCustomMenu()
        {
            customMenu.SelectedItemIndex = 0;

            customMenu.Visible = false;
            customMenu.Enabled = false;
        }
        
        private void CheckButtonClick()
        {
            mouseState = Mouse.GetState();
            clicked = false;

            if (mouseState.LeftButton == ButtonState.Pressed)
                pressed = true;
            if (mouseState.LeftButton == ButtonState.Released && pressed)
            {
                clicked = true;
                pressed = false;
            }
        }

        public int CustomSelectedMenuIndex
        {
            get { return customMenu.SelectedItemIndex; }
        }
        
        /// <summary>
        /// SetWindow checks the mouse position relative to the hard-coded fields for each tab and changes the activeWindow
        /// </summary>
        private void SetWindow()
        {
            CheckButtonClick();
            
            mouseState = Mouse.GetState();            

            if (mouseState.X > rangeWidth.X && mouseState.X < rangeWidth.Y && clicked)
            {
                if (mouseState.Y > rangeEasy.X && mouseState.Y < rangeEasy.Y)
                {
                    audioManager.PlayCue("btnclick6");
                    windowName = "easyWindow";
                    activeWindow = easyWindow;
                }

                if (mouseState.Y > rangeMedium.X && mouseState.Y < rangeMedium.Y)
                {
                    audioManager.PlayCue("btnclick6");
                    windowName = "mediumWindow";
                    activeWindow = mediumWindow;
                }

                if (mouseState.Y > rangeHard.X && mouseState.Y < rangeHard.Y)
                {
                    audioManager.PlayCue("btnclick6");
                    windowName = "hardWindow";
                    activeWindow = hardWindow;
                }

                if (mouseState.Y > rangeCustom.X && mouseState.Y < rangeCustom.Y)
                {
                    if (quickPlay == false)
                    {
                        audioManager.PlayCue("btnclick6");
                        windowName = "customWindow";
                        activeWindow = customWindow;
                    }
                }
            }
        }
        
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            SetWindow();

            if (activeWindow == customWindow)
                ShowCustomMenu();
            else
                HideCustomMenu();

            base.Update(gameTime);
        }

        /// <summary>
        /// Draws the activeWindow and all of the tabs
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Draw(background, new Rectangle(0, 0, Game.Window.ClientBounds.Width,
                        Game.Window.ClientBounds.Height), Color.White);
            
            spriteBatch.Draw(easyTab,   new Rectangle(100, 178, 150, 155), Color.White);
            spriteBatch.Draw(mediumTab, new Rectangle(100, 344, 150, 155), Color.White);
            spriteBatch.Draw(hardTab,   new Rectangle(100, 509, 150, 154), Color.White);
            if (quickPlay)
                spriteBatch.Draw(customTab, new Rectangle(100, 674, 150, 155), Color.DarkGray);
            else
                spriteBatch.Draw(customTab, new Rectangle(100, 674, 150, 155), Color.White);

            spriteBatch.Draw(activeWindow,       new Rectangle(240, 130, 700, 750), Color.White);

            base.Draw(gameTime);
        }
    }
}
